<html>

<head>
    <meta charset="UTF-8">
    <title>demo</title>
</head>
<body>
<h1>demo</h1>
</body>

<script type="text/javascript">

    var webSocket = null;
    if ("WebSocket" in window) {
        webSocket = new WebSocket('ws://localhost:8080/demo/webSocket');
    } else {
        alert("浏览器不支持websocket");
    }
    webSocket.onopen = function (e) {
        console.log("建立连接");
    }
    webSocket.onmessage = function (e) {
        console.log("收到消息" + e.data);
        alert(e.data);
    }
    webSocket.onclose = function (e) {
        console.log("关闭连接");
    }
    webSocket.onerror = function (e) {
        console.log(JSON.stringify(e));
    }
</script>
</html>